1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module soup.MessageHeaders; 26 27 private import glib.ConstructionException; 28 private import glib.HashTable; 29 private import glib.Str; 30 private import glib.c.functions; 31 private import gobject.ObjectG; 32 private import linker.Loader; 33 private import soup.c.functions; 34 public import soup.c.types; 35 36 37 /** 38 * The HTTP message headers associated with a request or response. 39 */ 40 public class MessageHeaders 41 { 42 /** the main Gtk struct */ 43 protected SoupMessageHeaders* soupMessageHeaders; 44 protected bool ownedRef; 45 46 /** Get the main Gtk struct */ 47 public SoupMessageHeaders* getMessageHeadersStruct(bool transferOwnership = false) 48 { 49 if (transferOwnership) 50 ownedRef = false; 51 return soupMessageHeaders; 52 } 53 54 /** the main Gtk struct as a void* */ 55 protected void* getStruct() 56 { 57 return cast(void*)soupMessageHeaders; 58 } 59 60 /** 61 * Sets our main struct and passes it to the parent class. 62 */ 63 public this (SoupMessageHeaders* soupMessageHeaders, bool ownedRef = false) 64 { 65 this.soupMessageHeaders = soupMessageHeaders; 66 this.ownedRef = ownedRef; 67 } 68 69 ~this () 70 { 71 if ( Linker.isLoaded(LIBRARY_SOUP[0]) && ownedRef ) 72 soup_message_headers_free(soupMessageHeaders); 73 } 74 75 76 /** */ 77 public static GType getType() 78 { 79 return soup_message_headers_get_type(); 80 } 81 82 /** 83 * Creates a #SoupMessageHeaders. (#SoupMessage does this 84 * automatically for its own headers. You would only need to use this 85 * method if you are manually parsing or generating message headers.) 86 * 87 * Params: 88 * type = the type of headers 89 * 90 * Returns: a new #SoupMessageHeaders 91 * 92 * Throws: ConstructionException GTK+ fails to create the object. 93 */ 94 public this(SoupMessageHeadersType type) 95 { 96 auto __p = soup_message_headers_new(type); 97 98 if(__p is null) 99 { 100 throw new ConstructionException("null returned by new"); 101 } 102 103 this(cast(SoupMessageHeaders*) __p); 104 } 105 106 /** 107 * Appends a new header with name @name and value @value to @hdrs. (If 108 * there is an existing header with name @name, then this creates a 109 * second one, which is only allowed for list-valued headers; see also 110 * soup_message_headers_replace().) 111 * 112 * The caller is expected to make sure that @name and @value are 113 * syntactically correct. 114 * 115 * Params: 116 * name = the header name to add 117 * value = the new value of @name 118 */ 119 public void append(string name, string value) 120 { 121 soup_message_headers_append(soupMessageHeaders, Str.toStringz(name), Str.toStringz(value)); 122 } 123 124 /** 125 * Removes all the headers listed in the Connection header. 126 * 127 * Since: 2.36 128 */ 129 public void cleanConnectionHeaders() 130 { 131 soup_message_headers_clean_connection_headers(soupMessageHeaders); 132 } 133 134 /** 135 * Clears @hdrs. 136 */ 137 public void clear() 138 { 139 soup_message_headers_clear(soupMessageHeaders); 140 } 141 142 alias foreac = foreach_; 143 /** 144 * Calls @func once for each header value in @hdrs. 145 * 146 * Beware that unlike soup_message_headers_get(), this processes the 147 * headers in exactly the way they were added, rather than 148 * concatenating multiple same-named headers into a single value. 149 * (This is intentional; it ensures that if you call 150 * soup_message_headers_append() multiple times with the same name, 151 * then the I/O code will output multiple copies of the header when 152 * sending the message to the remote implementation, which may be 153 * required for interoperability in some cases.) 154 * 155 * You may not modify the headers from @func. 156 * 157 * Params: 158 * func = callback function to run for each header 159 * userData = data to pass to @func 160 */ 161 public void foreach_(SoupMessageHeadersForeachFunc func, void* userData) 162 { 163 soup_message_headers_foreach(soupMessageHeaders, func, userData); 164 } 165 166 /** 167 * Frees @hdrs. 168 */ 169 public void free() 170 { 171 soup_message_headers_free(soupMessageHeaders); 172 ownedRef = false; 173 } 174 175 /** 176 * Frees the array of ranges returned from soup_message_headers_get_ranges(). 177 * 178 * Params: 179 * ranges = an array of #SoupRange 180 * 181 * Since: 2.26 182 */ 183 public void freeRanges(SoupRange* ranges) 184 { 185 soup_message_headers_free_ranges(soupMessageHeaders, ranges); 186 } 187 188 /** 189 * Gets the value of header @name in @hdrs. 190 * 191 * This method was supposed to work correctly for both single-valued 192 * and list-valued headers, but because some HTTP clients/servers 193 * mistakenly send multiple copies of headers that are supposed to be 194 * single-valued, it sometimes returns incorrect results. To fix this, 195 * the methods soup_message_headers_get_one() and 196 * soup_message_headers_get_list() were introduced, so callers can 197 * explicitly state which behavior they are expecting. 198 * 199 * Deprecated: Use soup_message_headers_get_one() or 200 * soup_message_headers_get_list() instead. 201 * 202 * Params: 203 * name = header name 204 * 205 * Returns: as with soup_message_headers_get_list(). 206 */ 207 public string get(string name) 208 { 209 return Str.toString(soup_message_headers_get(soupMessageHeaders, Str.toStringz(name))); 210 } 211 212 /** 213 * Looks up the "Content-Disposition" header in @hdrs, parses it, and 214 * returns its value in *@disposition and *@params. @params can be 215 * %NULL if you are only interested in the disposition-type. 216 * 217 * In HTTP, the most common use of this header is to set a 218 * disposition-type of "attachment", to suggest to the browser that a 219 * response should be saved to disk rather than displayed in the 220 * browser. If @params contains a "filename" parameter, this is a 221 * suggestion of a filename to use. (If the parameter value in the 222 * header contains an absolute or relative path, libsoup will truncate 223 * it down to just the final path component, so you do not need to 224 * test this yourself.) 225 * 226 * Content-Disposition is also used in "multipart/form-data", however 227 * this is handled automatically by #SoupMultipart and the associated 228 * form methods. 229 * 230 * Params: 231 * disposition = return location for the 232 * disposition-type, or %NULL 233 * params = return 234 * location for the Content-Disposition parameters, or %NULL 235 * 236 * Returns: %TRUE if @hdrs contains a "Content-Disposition" 237 * header, %FALSE if not (in which case *@disposition and *@params 238 * will be unchanged). 239 * 240 * Since: 2.26 241 */ 242 public bool getContentDisposition(out string disposition, out HashTable params) 243 { 244 char* outdisposition = null; 245 GHashTable* outparams = null; 246 247 auto __p = soup_message_headers_get_content_disposition(soupMessageHeaders, &outdisposition, &outparams) != 0; 248 249 disposition = Str.toString(outdisposition); 250 params = new HashTable(outparams); 251 252 return __p; 253 } 254 255 /** 256 * Gets the message body length that @hdrs declare. This will only 257 * be non-0 if soup_message_headers_get_encoding() returns 258 * %SOUP_ENCODING_CONTENT_LENGTH. 259 * 260 * Returns: the message body length declared by @hdrs. 261 */ 262 public long getContentLength() 263 { 264 return soup_message_headers_get_content_length(soupMessageHeaders); 265 } 266 267 /** 268 * Parses @hdrs's Content-Range header and returns it in @start, 269 * @end, and @total_length. If the total length field in the header 270 * was specified as "*", then @total_length will be set to -1. 271 * 272 * Params: 273 * start = return value for the start of the range 274 * end = return value for the end of the range 275 * totalLength = return value for the total length of the 276 * resource, or %NULL if you don't care. 277 * 278 * Returns: %TRUE if @hdrs contained a "Content-Range" header 279 * containing a byte range which could be parsed, %FALSE otherwise. 280 * 281 * Since: 2.26 282 */ 283 public bool getContentRange(out long start, out long end, out long totalLength) 284 { 285 return soup_message_headers_get_content_range(soupMessageHeaders, &start, &end, &totalLength) != 0; 286 } 287 288 /** 289 * Looks up the "Content-Type" header in @hdrs, parses it, and returns 290 * its value in *@content_type and *@params. @params can be %NULL if you 291 * are only interested in the content type itself. 292 * 293 * Params: 294 * params = return location for the Content-Type parameters (eg, "charset"), or 295 * %NULL 296 * 297 * Returns: a string with the value of the 298 * "Content-Type" header or %NULL if @hdrs does not contain that 299 * header or it cannot be parsed (in which case *@params will be 300 * unchanged). 301 * 302 * Since: 2.26 303 */ 304 public string getContentType(out HashTable params) 305 { 306 GHashTable* outparams = null; 307 308 auto retStr = soup_message_headers_get_content_type(soupMessageHeaders, &outparams); 309 310 params = new HashTable(outparams); 311 312 return Str.toString(retStr); 313 } 314 315 /** 316 * Gets the message body encoding that @hdrs declare. This may not 317 * always correspond to the encoding used on the wire; eg, a HEAD 318 * response may declare a Content-Length or Transfer-Encoding, but 319 * it will never actually include a body. 320 * 321 * Returns: the encoding declared by @hdrs. 322 */ 323 public SoupEncoding getEncoding() 324 { 325 return soup_message_headers_get_encoding(soupMessageHeaders); 326 } 327 328 /** 329 * Gets the expectations declared by @hdrs's "Expect" header. 330 * Currently this will either be %SOUP_EXPECTATION_CONTINUE or 331 * %SOUP_EXPECTATION_UNRECOGNIZED. 332 * 333 * Returns: the contents of @hdrs's "Expect" header 334 */ 335 public SoupExpectation getExpectations() 336 { 337 return soup_message_headers_get_expectations(soupMessageHeaders); 338 } 339 340 /** 341 * Gets the type of headers. 342 * 343 * Returns: the header's type. 344 * 345 * Since: 2.50 346 */ 347 public SoupMessageHeadersType getHeadersType() 348 { 349 return soup_message_headers_get_headers_type(soupMessageHeaders); 350 } 351 352 /** 353 * Gets the value of header @name in @hdrs. Use this for headers whose 354 * values are comma-delimited lists, and which are therefore allowed 355 * to appear multiple times in the headers. For non-list-valued 356 * headers, use soup_message_headers_get_one(). 357 * 358 * If @name appears multiple times in @hdrs, 359 * soup_message_headers_get_list() will concatenate all of the values 360 * together, separated by commas. This is sometimes awkward to parse 361 * (eg, WWW-Authenticate, Set-Cookie), but you have to be able to deal 362 * with it anyway, because the HTTP spec explicitly states that this 363 * transformation is allowed, and so an upstream proxy could do the 364 * same thing. 365 * 366 * Params: 367 * name = header name 368 * 369 * Returns: the header's value or %NULL if not found. 370 * 371 * Since: 2.28 372 */ 373 public string getList(string name) 374 { 375 return Str.toString(soup_message_headers_get_list(soupMessageHeaders, Str.toStringz(name))); 376 } 377 378 /** 379 * Gets the value of header @name in @hdrs. Use this for headers whose 380 * values are <emphasis>not</emphasis> comma-delimited lists, and 381 * which therefore can only appear at most once in the headers. For 382 * list-valued headers, use soup_message_headers_get_list(). 383 * 384 * If @hdrs does erroneously contain multiple copies of the header, it 385 * is not defined which one will be returned. (Ideally, it will return 386 * whichever one makes libsoup most compatible with other HTTP 387 * implementations.) 388 * 389 * Params: 390 * name = header name 391 * 392 * Returns: the header's value or %NULL if not found. 393 * 394 * Since: 2.28 395 */ 396 public string getOne(string name) 397 { 398 return Str.toString(soup_message_headers_get_one(soupMessageHeaders, Str.toStringz(name))); 399 } 400 401 /** 402 * Parses @hdrs's Range header and returns an array of the requested 403 * byte ranges. The returned array must be freed with 404 * soup_message_headers_free_ranges(). 405 * 406 * If @total_length is non-0, its value will be used to adjust the 407 * returned ranges to have explicit start and end values, and the 408 * returned ranges will be sorted and non-overlapping. If 409 * @total_length is 0, then some ranges may have an end value of -1, 410 * as described under #SoupRange, and some of the ranges may be 411 * redundant. 412 * 413 * Beware that even if given a @total_length, this function does not 414 * check that the ranges are satisfiable. 415 * 416 * <note><para> 417 * #SoupServer has built-in handling for range requests. If your 418 * server handler returns a %SOUP_STATUS_OK response containing the 419 * complete response body (rather than pausing the message and 420 * returning some of the response body later), and there is a Range 421 * header in the request, then libsoup will automatically convert the 422 * response to a %SOUP_STATUS_PARTIAL_CONTENT response containing only 423 * the range(s) requested by the client. 424 * 425 * The only time you need to process the Range header yourself is if 426 * either you need to stream the response body rather than returning 427 * it all at once, or you do not already have the complete response 428 * body available, and only want to generate the parts that were 429 * actually requested by the client. 430 * </para></note> 431 * 432 * Params: 433 * totalLength = the total_length of the response body 434 * ranges = return location for an array 435 * of #SoupRange 436 * 437 * Returns: %TRUE if @hdrs contained a syntactically-valid 438 * "Range" header, %FALSE otherwise (in which case @range and @length 439 * will not be set). 440 * 441 * Since: 2.26 442 */ 443 public bool getRanges(long totalLength, out SoupRange[] ranges) 444 { 445 SoupRange* outranges; 446 int length; 447 448 auto __p = soup_message_headers_get_ranges(soupMessageHeaders, totalLength, &outranges, &length) != 0; 449 450 ranges = outranges[0 .. length]; 451 452 return __p; 453 } 454 455 /** 456 * Checks whether the list-valued header @name is present in @hdrs, 457 * and contains a case-insensitive match for @token. 458 * 459 * (If @name is present in @hdrs, then this is equivalent to calling 460 * soup_header_contains() on its value.) 461 * 462 * Params: 463 * name = header name 464 * token = token to look for 465 * 466 * Returns: %TRUE if the header is present and contains @token, 467 * %FALSE otherwise. 468 * 469 * Since: 2.50 470 */ 471 public bool headerContains(string name, string token) 472 { 473 return soup_message_headers_header_contains(soupMessageHeaders, Str.toStringz(name), Str.toStringz(token)) != 0; 474 } 475 476 /** 477 * Checks whether the header @name is present in @hdrs and is 478 * (case-insensitively) equal to @value. 479 * 480 * Params: 481 * name = header name 482 * value = expected value 483 * 484 * Returns: %TRUE if the header is present and its value is 485 * @value, %FALSE otherwise. 486 * 487 * Since: 2.50 488 */ 489 public bool headerEquals(string name, string value) 490 { 491 return soup_message_headers_header_equals(soupMessageHeaders, Str.toStringz(name), Str.toStringz(value)) != 0; 492 } 493 494 /** 495 * Removes @name from @hdrs. If there are multiple values for @name, 496 * they are all removed. 497 * 498 * Params: 499 * name = the header name to remove 500 */ 501 public void remove(string name) 502 { 503 soup_message_headers_remove(soupMessageHeaders, Str.toStringz(name)); 504 } 505 506 /** 507 * Replaces the value of the header @name in @hdrs with @value. (See 508 * also soup_message_headers_append().) 509 * 510 * The caller is expected to make sure that @name and @value are 511 * syntactically correct. 512 * 513 * Params: 514 * name = the header name to replace 515 * value = the new value of @name 516 */ 517 public void replace(string name, string value) 518 { 519 soup_message_headers_replace(soupMessageHeaders, Str.toStringz(name), Str.toStringz(value)); 520 } 521 522 /** 523 * Sets the "Content-Disposition" header in @hdrs to @disposition, 524 * optionally with additional parameters specified in @params. 525 * 526 * See soup_message_headers_get_content_disposition() for a discussion 527 * of how Content-Disposition is used in HTTP. 528 * 529 * Params: 530 * disposition = the disposition-type 531 * params = additional 532 * parameters, or %NULL 533 * 534 * Since: 2.26 535 */ 536 public void setContentDisposition(string disposition, HashTable params) 537 { 538 soup_message_headers_set_content_disposition(soupMessageHeaders, Str.toStringz(disposition), (params is null) ? null : params.getHashTableStruct()); 539 } 540 541 /** 542 * Sets the message body length that @hdrs will declare, and sets 543 * @hdrs's encoding to %SOUP_ENCODING_CONTENT_LENGTH. 544 * 545 * You do not normally need to call this; if @hdrs is set to use 546 * Content-Length encoding, libsoup will automatically set its 547 * Content-Length header for you immediately before sending the 548 * headers. One situation in which this method is useful is when 549 * generating the response to a HEAD request; Calling 550 * soup_message_headers_set_content_length() allows you to put the 551 * correct content length into the response without needing to waste 552 * memory by filling in a response body which won't actually be sent. 553 * 554 * Params: 555 * contentLength = the message body length 556 */ 557 public void setContentLength(long contentLength) 558 { 559 soup_message_headers_set_content_length(soupMessageHeaders, contentLength); 560 } 561 562 /** 563 * Sets @hdrs's Content-Range header according to the given values. 564 * (Note that @total_length is the total length of the entire resource 565 * that this is a range of, not simply @end - @start + 1.) 566 * 567 * <note><para> 568 * #SoupServer has built-in handling for range requests, and you do 569 * not normally need to call this function youself. See 570 * soup_message_headers_get_ranges() for more details. 571 * </para></note> 572 * 573 * Params: 574 * start = the start of the range 575 * end = the end of the range 576 * totalLength = the total length of the resource, or -1 if unknown 577 * 578 * Since: 2.26 579 */ 580 public void setContentRange(long start, long end, long totalLength) 581 { 582 soup_message_headers_set_content_range(soupMessageHeaders, start, end, totalLength); 583 } 584 585 /** 586 * Sets the "Content-Type" header in @hdrs to @content_type, 587 * optionally with additional parameters specified in @params. 588 * 589 * Params: 590 * contentType = the MIME type 591 * params = additional 592 * parameters, or %NULL 593 * 594 * Since: 2.26 595 */ 596 public void setContentType(string contentType, HashTable params) 597 { 598 soup_message_headers_set_content_type(soupMessageHeaders, Str.toStringz(contentType), (params is null) ? null : params.getHashTableStruct()); 599 } 600 601 /** 602 * Sets the message body encoding that @hdrs will declare. In particular, 603 * you should use this if you are going to send a request or response in 604 * chunked encoding. 605 * 606 * Params: 607 * encoding = a #SoupEncoding 608 */ 609 public void setEncoding(SoupEncoding encoding) 610 { 611 soup_message_headers_set_encoding(soupMessageHeaders, encoding); 612 } 613 614 /** 615 * Sets @hdrs's "Expect" header according to @expectations. 616 * 617 * Currently %SOUP_EXPECTATION_CONTINUE is the only known expectation 618 * value. You should set this value on a request if you are sending a 619 * large message body (eg, via POST or PUT), and want to give the 620 * server a chance to reject the request after seeing just the headers 621 * (eg, because it will require authentication before allowing you to 622 * post, or because you're POSTing to a URL that doesn't exist). This 623 * saves you from having to transmit the large request body when the 624 * server is just going to ignore it anyway. 625 * 626 * Params: 627 * expectations = the expectations to set 628 */ 629 public void setExpectations(SoupExpectation expectations) 630 { 631 soup_message_headers_set_expectations(soupMessageHeaders, expectations); 632 } 633 634 /** 635 * Sets @hdrs's Range header to request the indicated range. 636 * @start and @end are interpreted as in a #SoupRange. 637 * 638 * If you need to request multiple ranges, use 639 * soup_message_headers_set_ranges(). 640 * 641 * Params: 642 * start = the start of the range to request 643 * end = the end of the range to request 644 * 645 * Since: 2.26 646 */ 647 public void setRange(long start, long end) 648 { 649 soup_message_headers_set_range(soupMessageHeaders, start, end); 650 } 651 652 /** 653 * Sets @hdrs's Range header to request the indicated ranges. (If you 654 * only want to request a single range, you can use 655 * soup_message_headers_set_range().) 656 * 657 * Params: 658 * ranges = an array of #SoupRange 659 * length = the length of @range 660 * 661 * Since: 2.26 662 */ 663 public void setRanges(SoupRange* ranges, int length) 664 { 665 soup_message_headers_set_ranges(soupMessageHeaders, ranges, length); 666 } 667 }